package pers.zh.parking.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import pers.zh.parking.common.BaseObserver;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.common.RxScheduler;
import pers.zh.parking.data.UserSettings;
import pers.zh.parking.data.entity.Car;
import pers.zh.parking.mvp.contract.MyCarsContract;
import pers.zjc.commonlibs.util.ObjectUtils;
import pers.zjc.commonlibs.util.StringUtils;

@ActivityScope
public class MyCarsPresenter extends BasePresenter<MyCarsContract.Model, MyCarsContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private UserSettings userSettings;

    @Inject
    public MyCarsPresenter(MyCarsContract.Model model, MyCarsContract.View rootView) {
        super(model, rootView);
        userSettings = UserSettings.getInstance();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void loadData() {
        if (StringUtils.isEmpty(userSettings.getUserId())) {
            return;
        }
        mModel.getCars(userSettings.getUserId())
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse<List<Car>>>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse<List<Car>> response) {
                      mRootView.showCars(response.getData());
                  }
              });
    }

    public void addCar(Car car) {
        if (!validate(car)) {
            return;
        }
        mModel.addCar(car)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("新增成功");
                      mRootView.success();
                  }
              });
    }

    public void updateCar(Car car) {
        if (car.getCarId() == null || car.getCarId() == 0) {
            mRootView.showMessage("车辆信息错误");
            return;
        }
        if (!validate(car)) {
            return;
        }
        mModel.updateCar(car)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("修改成功");
                      mRootView.success();
                  }
              });
    }

    public void delete(List<Car> cars) {
        if (ObjectUtils.isEmpty(cars)) {
            mRootView.showMessage("要删除的车辆不能为空");
            return;
        }
        mModel.delete(cars)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("删除成功");
                      mRootView.delSuccess(cars);
                  }
              });
    }

    private boolean validate(Car car) {
        if (StringUtils.isEmpty(car.getCarNo())) {
            mRootView.showMessage("车牌不能为空");
            return false;
        }
        if (StringUtils.isEmpty(car.getCarColor())) {
            mRootView.showMessage("汽车颜色不能为空");
            return false;
        }
        if (StringUtils.isEmpty(car.getCarBrand())) {
            mRootView.showMessage("汽车品牌不能为空");
            return false;
        }
        if (StringUtils.isEmpty(userSettings.getUserId())) {
            mRootView.showMessage("登录信息错误");
            return false;
        }
        car.setUserId(userSettings.getUserId());
        return true;
    }

}
